<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 异步处理完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            line-height: 1.6;
        }
        
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: bold;
            margin-right: 15px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px 30px;
            border-radius: 15px;
            margin: 30px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(0.8); opacity: 0.5; }
            50% { transform: scale(1.2); opacity: 0.8; }
        }
        
        .feature-card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }
        
        .feature-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Spring Boot 异步处理
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">完全指南</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    掌握 @Async 注解，提升应用性能，优化并发处理
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-rocket text-4xl mb-2"></i>
                        <p class="text-sm">高性能</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-layer-group text-4xl mb-2"></i>
                        <p class="text-sm">异步处理</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-code text-4xl mb-2"></i>
                        <p class="text-sm">简单实现</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="bg-white rounded-3xl shadow-xl p-10 mb-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Spring Boot 中实现异步处理主要依赖于 Spring 框架提供的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解。异步处理允许某些任务在单独的线程中执行，而不是阻塞主线程，从而提高应用程序的性能和响应速度。异步处理能够提高应用程序的响应速度和吞吐量，适用于耗时操作或并发任务场景。
                </p>
            </div>

            <!-- Async Flow Diagram -->
            <div class="mb-12">
                <h2 class="text-3xl font-bold text-gray-800 mb-6 text-center">异步处理流程图</h2>
                <div class="mermaid">
                    graph LR
                        A[客户端请求] --> B[Controller]
                        B --> C{是否异步?}
                        C -->|是| D[@Async方法]
                        C -->|否| E[同步处理]
                        D --> F[线程池]
                        F --> G[异步执行]
                        G --> H[返回Future/CompletableFuture]
                        E --> I[直接返回结果]
                        H --> J[客户端获取结果]
                        I --> J
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style D fill:#bbf,stroke:#333,stroke-width:2px
                        style F fill:#bfb,stroke:#333,stroke-width:2px
                </div>
            </div>

            <!-- Section 1 -->
            <div class="feature-card card-hover mb-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                    <span class="section-number">1</span>
                    配置异步支持
                </h3>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    首先，需要在 Spring Boot 项目中启用异步支持。可以通过在配置类上添加 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@EnableAsync</code> 注解来实现：
                </p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>AsyncConfig.java
                    </div>
                    <pre><code>import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {
    // 可以在这里配置异步执行器（Executor）等其他异步相关的配置
}</code></pre>
                </div>
                <div class="mt-6 bg-blue-50 border-l-4 border-blue-500 p-4 rounded">
                    <p class="text-blue-800">
                        <i class="fas fa-info-circle mr-2"></i>
                        <code>@EnableAsync</code> 注解启用了 Spring 的异步方法执行功能。
                    </p>
                </div>
            </div>

            <!-- Section 2 -->
            <div class="feature-card card-hover mb-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                    <span class="section-number">2</span>
                    使用 @Async 注解实现异步方法
                </h3>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    在需要异步执行的方法上添加 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解。此方法会在一个单独的线程中运行，而不会阻塞调用它的线程。
                </p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>AsyncService.java
                    </div>
                    <pre><code>import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {

    @Async
    public void performAsyncTask() {
        try {
            System.out.println("开始异步任务");
            Thread.sleep(5000); // 模拟耗时操作
            System.out.println("异步任务完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}</code></pre>
                </div>
                <p class="text-gray-700 mt-6 leading-relaxed">
                    在上面的示例中，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">performAsyncTask()</code> 方法会在一个单独的线程中执行，而不会阻塞主线程。
                </p>
            </div>

            <!-- Key Features -->
            <div class="highlight-box mb-12">
                <h3 class="text-2xl font-bold mb-4">
                    <i class="fas fa-star mr-3"></i>异步处理的核心优势
                </h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-6">
                    <div class="bg-white bg-opacity-20 rounded-lg p-4">
                        <i class="fas fa-tachometer-alt text-3xl mb-2"></i>
                        <h4 class="font-bold mb-2">提升性能</h4>
                        <p class="text-sm">避免线程阻塞，提高系统吞吐量</p>
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg p-4">
                        <i class="fas fa-users text-3xl mb-2"></i>
                        <h4 class="font-bold mb-2">改善用户体验</h4>
                        <p class="text-sm">快速响应用户请求，后台处理耗时任务</p>
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg p-4">
                        <i class="fas fa-cogs text-3xl mb-2"></i>
                        <h4 class="font-bold mb-2">资源优化</h4>
                        <p class="text-sm">合理利用系统资源，提高并发处理能力</p>
                    </div>
                </div>
            </div>

            <!-- Section 3 -->
            <div class="feature-card card-hover mb-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                    <span class="section-number">3</span>
                    自定义线程池
                </h3>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    默认情况下，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">@Async</code> 注解使用的是 Spring 提供的默认线程池。如果你需要自定义线程池，可以在配置类中定义一个 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Executor</code> Bean：
                </p>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>AsyncConfig.java
                    </div>
                    <pre><code>import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);  // 核心线程数
        executor